﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameSPK.Model;
using System.IO;
using System.Xml.Linq;

namespace GameSPK.Helpers
{
    class CrossWordHelp
    {
        public static CrossWord getCrossWord(string filename)
        {
            string fileXML = filename;
            CrossWord crossWord = new CrossWord();
            if (File.Exists(fileXML))
            {
                XElement layout = XElement.Load(fileXML);
                var home = (from c in layout.Elements()
                            where c.Name == "CrossWord"
                            select c).Single();
                crossWord.chude = home.Attribute("ChuDe").Value;
                crossWord.dapan = home.Attribute("DapAn").Value;
                crossWord.sohangngang = int.Parse(home.Attribute("SoHangNgang").Value);
                return crossWord;
            }
            return null;
        }
        public static HangNgangStart getHangNgangi(string filename, int so)
        {
            string fileXML =  filename;
            HangNgangStart crossWord = new HangNgangStart();
            string nametab = ("HangNgang" + so);
            if (File.Exists(fileXML))
            {
                XElement layout = XElement.Load(fileXML);
                var home = (from c in layout.Element("HangNgang").Elements()
                            where c.Name == nametab 
                            select c).Single();
                crossWord.CauHoi = home.Attribute("CauHoi").Value;
                crossWord.OHangNgang = home.Attribute("OHangNgang").Value;
                crossWord.KeyChar = (home.Attribute("KeyChar").Value);
                return crossWord;
            }
            return null;
        }
        public static LabelLayout getClockCrossWordFromXML(string filename)
        {
            string fileXML = Helper.folderLayout + "\\" + filename;
            LabelLayout video = new LabelLayout();

            if (File.Exists(fileXML))
            {
                XElement layout = XElement.Load(fileXML);
                var home = (from c in layout.Elements()
                            where c.Name == "Clock"
                            select c).Single();
                video.Width = double.Parse(home.Attribute("Width").Value);
                video.Height = double.Parse(home.Attribute("Height").Value);
                video.FontSize = double.Parse(home.Attribute("FontSize").Value);

                video.sBackground = home.Attribute("BackClock").Value;
                video.sForeground = home.Attribute("ClockText").Value;
                video.iTime1 = int.Parse(home.Attribute("TimeHang").Value);
                video.iTime2 = int.Parse(home.Attribute("TimeTuKhoa").Value);


                video.Left = double.Parse(home.Attribute("Left").Value);
                video.Top = double.Parse(home.Attribute("Top").Value);

                return video;
            }
            return null;

        }
        public static CrossWordLayOut getCrossWordLayoutFromXML(string filename)
        {
            string fileXML = Helper.folderLayout + "\\" + filename;
            CrossWordLayOut video = new CrossWordLayOut();

            if (File.Exists(fileXML))
            {
                XElement layout = XElement.Load(fileXML);
                var home = (from c in layout.Elements()
                            where c.Name == "CrossWord"
                            select c).Single();
                video.Width = double.Parse(home.Attribute("Width").Value);
                video.Height = double.Parse(home.Attribute("Height").Value);
                video.FontSize = double.Parse(home.Attribute("FontSize").Value);

                video.sColorKey = home.Attribute("ColorKey").Value;
                video.sColorNotKey = home.Attribute("ColorNotKey").Value;
                video.sBackgroundClose = home.Attribute("BackgroundClose").Value;
                video.sBackgroundOpen = home.Attribute("BackgroundOpen").Value;
                video.sBackgroundSelected = home.Attribute("BackgroundSelected").Value;
                video.sBackgroundStart = home.Attribute("BackgroundStart").Value;
                
                video.Left = double.Parse(home.Attribute("Left").Value);
                video.Top = double.Parse(home.Attribute("Top").Value);

                return video;
            }
            return null;
        }
        
    }
}
